package com.atguigu.schedule.service.impl;

import com.atguigu.schedule.mapper.SysUserMapper;
import com.atguigu.schedule.pojo.SysUser;
import com.atguigu.schedule.service.SysUserService;
import com.atguigu.schedule.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {
    
    @Autowired
    private SysUserMapper userMapper;

    @Override
    public int regist(SysUser sysUser) {
        // 检查用户名是否已存在
        if (userMapper.existsByUsername(sysUser.getUsername())) {
            return 0; // 用户名已存在
        }
        
        // 将用户的明文密码转化为密文
        sysUser.setUserPwd(MD5Util.encrypt(sysUser.getUserPwd()));
        
        // 注意：SysUser实体类没有createdAt和updatedAt字段
        
        // 保存用户信息到数据库
        int result = userMapper.insert(sysUser);
        return result > 0 ? 1 : 0;
    }

    @Override
    public SysUser findByUsername(String username) {
        return userMapper.selectByUsername(username);
    }
}
